﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.Pop
{
    public partial class WorkOrder : Common.PageBase
    {
        private UserLoginInfo _userLoginInfo;
        T_DICTIONARY_BLL dBll = new T_DICTIONARY_BLL();
        T_LOCATIONINFORMATION_BLL liBll = new T_LOCATIONINFORMATION_BLL();
        T_DEALER_BLL dealerBll = new T_DEALER_BLL();
        T_CUSTOMER_BLL cBll = new T_CUSTOMER_BLL();
        private T_SALEVEHICLE_BLL saleVehicleBll = new T_SALEVEHICLE_BLL();
        private T_CONFIG_BLL configBll = new T_CONFIG_BLL();
        DateTime beginDateTime;
        

        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            beginDateTime = DateTime.Now;

            UserLoginInfo.CheckLoginInfo();
            _userLoginInfo = UserLoginInfo.GetLoginInfo();
            UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + _userLoginInfo.UserEntity.USERID.ToString(), Request.RawUrl.ToString());

            consultingCase.ParentPage = this.Page;
            consultingCase.PlaceHolderCustomerInfo = this.phCustomerInfo;
            consultingCase.UserLoginInfo = _userLoginInfo;
            repairCase.ParentPage = this.Page;
            repairCase.PlaceHolderCustomerInfo = this.phCustomerInfo;
            repairCase.UserLoginInfo = _userLoginInfo;
            complaintCase.ParentPage = this.Page;
            complaintCase.PlaceHolderCustomerInfo = this.phCustomerInfo;
            complaintCase.UserLoginInfo = _userLoginInfo;
            potentialCustomerCase.ParentPage = this.Page;
            potentialCustomerCase.PlaceHolderCustomerInfo = this.phCustomerInfo;
            potentialCustomerCase.UserLoginInfo = _userLoginInfo;

        }

        protected void Page_Load(object sender, EventArgs e)
        {

            UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + _userLoginInfo.UserEntity.USERID.ToString(), "WorkOrder_Page_LoadBegin");
            if (!IsPostBack)
            {
                txtGPSUrl.Text = configBll.GetCacheValueByName(this.Page, "GPSUrl");
                ddlWorkOrderTypeBinding();
                ddlDEALERIDBinding();
                ddlCUSTOMERBASEBinding();
                ddlCUSTOMERTYPEBinding();
                ddlCUSTOMERLEVELBinding();
                ddlSTATUSBinding();
                ddlBIGCUSTOMERTYPEBinding();




                pageDataInit();



            }

            btnAddComplaintCase.Visible = UserLoginInfo.IsHasPermissionRight(StaticConstant.PopPageClass, "AddComplaintCase");
            btnAddConsultingCase.Visible = UserLoginInfo.IsHasPermissionRight(StaticConstant.PopPageClass, "AddConsultingCase");
            btnAddRepairCase.Visible = UserLoginInfo.IsHasPermissionRight(StaticConstant.PopPageClass, "AddRepairCase");
            
            UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + _userLoginInfo.UserEntity.USERID.ToString(), "WorkOrder_Page_LoadEnd");

        }



        #region 页面初始化数据绑定

        /// <summary>
        /// DDLs the work order type binding.
        /// </summary>
        private void ddlWorkOrderTypeBinding()
        {
            ddlWorkOrderType.Items.Clear();
            EnumHelper.FillListControl(ddlWorkOrderType, typeof(WorkOrderType));

        }
       
        /// <summary>
        /// DDLs the CUSTOMERBASE binding.
        /// </summary>
        private void ddlCUSTOMERBASEBinding()
        {
            ddlCUSTOMERBASE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CustomerBase"));
            ddlCUSTOMERBASE.DataTextField = "DictValue";
            ddlCUSTOMERBASE.DataValueField = "DictKey";
            ddlCUSTOMERBASE.DataBind();
            ddlCUSTOMERBASE.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the CUSTOMERTYPE binding.
        /// </summary>
        private void ddlCUSTOMERTYPEBinding()
        {
            ddlCUSTOMERTYPE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CustomerType"));
            ddlCUSTOMERTYPE.DataTextField = "DictValue";
            ddlCUSTOMERTYPE.DataValueField = "DictKey";
            ddlCUSTOMERTYPE.DataBind();
            ddlCUSTOMERTYPE.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// DDLs the CUSTOMERLEVEL binding.
        /// </summary>
        private void ddlCUSTOMERLEVELBinding()
        {
            ddlCUSTOMERLEVEL.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CustomerLevel"));
            ddlCUSTOMERLEVEL.DataTextField = "DictValue";
            ddlCUSTOMERLEVEL.DataValueField = "DictKey";
            ddlCUSTOMERLEVEL.DataBind();
            ddlCUSTOMERLEVEL.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the BIGCUSTOMERTYPE binding.
        /// </summary>
        private void ddlBIGCUSTOMERTYPEBinding()
        {
            ddlBIGCUSTOMERTYPE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("BigCustomerType"));
            ddlBIGCUSTOMERTYPE.DataTextField = "DictValue";
            ddlBIGCUSTOMERTYPE.DataValueField = "DictKey";
            ddlBIGCUSTOMERTYPE.DataBind();
            ddlBIGCUSTOMERTYPE.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the STATUS binding.
        /// </summary>
        private void ddlSTATUSBinding()
        {
            

            EnumHelper.FillListControl(ddlSTATUS, typeof(CustomerStatus));
        }

        /// <summary>
        /// DDLs the COUNTRY binding.
        /// </summary>
        private void ddlCOUNTRYBinding()
        {

            ddlCOUNTRY.DataSource = liBll.GetCacheListByParentCode(this.Page, "");
            ddlCOUNTRY.DataTextField = "NAME";
            ddlCOUNTRY.DataValueField = "CODE";
            ddlCOUNTRY.DataBind();
            ddlCOUNTRY.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// DDLs the PROVINCE binding.
        /// </summary>
        private void ddlPROVINCEBinding()
        {
            ddlPROVINCE.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlCOUNTRY.SelectedValue.Trim());
            ddlPROVINCE.DataTextField = "NAME";
            ddlPROVINCE.DataValueField = "CODE";
            ddlPROVINCE.DataBind();
            ddlPROVINCE.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// DDLs the PREFECTURECITY binding.
        /// </summary>
        private void ddlPREFECTURECITYBinding()
        {
            ddlPREFECTURECITY.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlPROVINCE.SelectedValue.Trim());
            ddlPREFECTURECITY.DataTextField = "NAME";
            ddlPREFECTURECITY.DataValueField = "CODE";
            ddlPREFECTURECITY.DataBind();
            ddlPREFECTURECITY.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// DDLs the CITY binding.
        /// </summary>
        private void ddlCITYBinding()
        {
            ddlCITY.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlPREFECTURECITY.SelectedValue.Trim());
            ddlCITY.DataTextField = "NAME";
            ddlCITY.DataValueField = "CODE";
            ddlCITY.DataBind();
            ddlCITY.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the VILLAGETOWN binding.
        /// </summary>
        private void ddlVILLAGETOWNBinding()
        {
            //ddlVILLAGETOWN.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlCITY.SelectedValue.Trim());
            //ddlVILLAGETOWN.DataTextField = "NAME";
            //ddlVILLAGETOWN.DataValueField = "CODE";
            //ddlVILLAGETOWN.DataBind();
            //ddlVILLAGETOWN.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 经销商编号 下拉列表数据绑定
        /// </summary>
        private void ddlDEALERIDBinding()
        {
            //string businessDivisionCode = (_userLoginInfo.UserEntity == null) ? "": _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;

            //ddlDEALERID.DataSource = dealerBll.GetList(string.Format(" and BUSINESSDIVISIONCODE = '{0}' ", businessDivisionCode));
            //ddlDEALERID.DataTextField = "DEALERNAME";
            //ddlDEALERID.DataValueField = "DEALERID";
            //ddlDEALERID.DataBind();
            //ddlDEALERID.Items.Insert(0, new ListItem("", ""));
        }

        #endregion

        #region DDL  事件
        protected void ddlCOUNTRY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlPROVINCEBinding();


        }

        protected void ddlPROVINCE_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlPREFECTURECITYBinding();
        }

        protected void ddlPREFECTURECITY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlCITYBinding();
        }

        protected void ddlCITY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlVILLAGETOWNBinding();
        }

        protected void ddlBUSINESSDIVISIONID_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDEALERIDBinding();
        }
        #endregion


        /// <summary>
        /// Pages the data init.
        /// </summary>
        private void pageDataInit()
        {
            string _vinCode = PageHelper.GetQueryString(this.Page, "VINCODE");
            string _customerid = String.IsNullOrEmpty(Request.QueryString["CUSTOMERID"]) ? "" : Request.QueryString["CUSTOMERID"].Trim();
            ddlWorkOrderType.SelectedValue = PageHelper.GetQueryString(this.Page, "CALLTYPE");

            if (!String.IsNullOrEmpty(_vinCode))
            {
                T_SALEVEHICLE_Entity saleVehicleEntity = saleVehicleBll.GetList(String.Format(" and VINCODE = '{0}' and  BUSINESSDIVISIONCODE = '{1}' ", _vinCode, _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE)).FirstOrDefault();
                if(saleVehicleEntity != null)
                {
                    T_CUSTOMER_Entity customerEntity = cBll.GetEntity("CUSTOMERID", saleVehicleEntity.CUSTOMERID);
                    if (customerEntity != null)
                    {
                        #region 客户地区级联

                        try
                        {
                            ddlCOUNTRYBinding();
                            ddlCOUNTRY.SelectedValue = customerEntity.COUNTRY;
                            ddlCOUNTRY_SelectedIndexChanged(null, null);
                            ddlPROVINCEBinding();
                            ddlPROVINCE.SelectedValue = customerEntity.PROVINCE;
                            ddlPROVINCE_SelectedIndexChanged(null, null);
                            ddlPREFECTURECITYBinding();
                            ddlPREFECTURECITY.SelectedValue = customerEntity.PREFECTURECITY;
                            ddlPREFECTURECITY_SelectedIndexChanged(null, null);
                            ddlCITYBinding();
                            ddlCITY.SelectedValue = customerEntity.CITY;
                            ddlCITY_SelectedIndexChanged(null, null);
                            ddlVILLAGETOWNBinding();
                            txtVILLAGETOWN.Text = customerEntity.VILLAGETOWN;
                        }
                        catch (Exception ex)
                        {
                        }

                        #endregion

                        PageHelper.SetControlValue<T_CUSTOMER_Entity>(this.phCustomerInfo, customerEntity);
                        lblCustomerCode.Text = customerEntity.CUSTOMERCODE;
                        hfCustomerId.Value = customerEntity.CUSTOMERID.Trim();
                    }
                        this.consultingCase.SaleVehicleEntity = saleVehicleEntity;
                        this.repairCase.SaleVehicleEntity = saleVehicleEntity;
                        this.complaintCase.SaleVehicleEntity = saleVehicleEntity;
                }

            }
            else
            {
                if (!String.IsNullOrEmpty(_customerid))
                {
                    T_CUSTOMER_Entity entity = cBll.GetEntity("CUSTOMERID", _customerid);
                    if (entity != null)
                    {
                        #region 客户地区级联

                        ddlCOUNTRYBinding();
                        ddlCOUNTRY.SelectedValue = entity.COUNTRY;
                        ddlCOUNTRY_SelectedIndexChanged(null, null);
                        ddlPROVINCEBinding();
                        ddlPROVINCE.SelectedValue = entity.PROVINCE;
                        ddlPROVINCE_SelectedIndexChanged(null, null);
                        ddlPREFECTURECITYBinding();
                        ddlPREFECTURECITY.SelectedValue = entity.PREFECTURECITY;
                        ddlPREFECTURECITY_SelectedIndexChanged(null, null);
                        ddlCITYBinding();
                        ddlCITY.SelectedValue = entity.CITY;
                        ddlCITY_SelectedIndexChanged(null, null);
                        ddlVILLAGETOWNBinding();
                        //ddlVILLAGETOWN.SelectedValue = entity.VILLAGETOWN;

                        #endregion

                        PageHelper.SetControlValue<T_CUSTOMER_Entity>(this.phCustomerInfo, entity);
                        lblCustomerCode.Text = entity.CUSTOMERCODE;
                        hfCustomerId.Value = entity.CUSTOMERID.Trim();
                    }
                }
                else
                {
                    txtFIRSTCONTACTTEL.Text = PageHelper.GetQueryString(this.Page, "PHONE");
                    ddlCOUNTRYBinding();
                    ddlPROVINCEBinding();
                    ddlPREFECTURECITYBinding();
                    ddlCITYBinding();
                    ddlVILLAGETOWNBinding();
                }
            }
            hfPhone.Value = PageHelper.GetQueryString(this.Page, "PHONE");
            hfCallId.Value = PageHelper.GetQueryString(this.Page, "CALLID");
            hfRecordCode.Value = PageHelper.GetQueryString(this.Page, "RECORDCODE");
            hfCallType.Value = PageHelper.GetQueryString(this.Page, "CALLTYPE");
            switch (PageHelper.GetQueryString(this.Page, "CALLTYPE"))
            {
                case "1":
                    {
                        lblCallType.Text = "咨询";
                    }
                    break;
                case "2":
                    {
                        lblCallType.Text = "报修";
                    }
                    break;
                case "3":
                    {
                        lblCallType.Text = "投诉";
                    }
                    break;
            }
            



        }

        protected void btnRefreshCustomerInfo_Click(object sender, EventArgs e)
        {
            string _customerid = hfCustomerId.Value;
            if (!String.IsNullOrEmpty(_customerid))
            {
                T_CUSTOMER_Entity entity = cBll.GetEntity("CUSTOMERID", _customerid);

                #region 客户地区级联

                try
                {
                    ddlCOUNTRYBinding();
                    ddlCOUNTRY.SelectedValue = entity.COUNTRY;
                    ddlCOUNTRY_SelectedIndexChanged(null, null);
                    ddlPROVINCEBinding();
                    ddlPROVINCE.SelectedValue = entity.PROVINCE;
                    ddlPROVINCE_SelectedIndexChanged(null, null);
                    ddlPREFECTURECITYBinding();
                    ddlPREFECTURECITY.SelectedValue = entity.PREFECTURECITY;
                    ddlPREFECTURECITY_SelectedIndexChanged(null, null);
                    ddlCITYBinding();
                    ddlCITY.SelectedValue = entity.CITY;
                    ddlCITY_SelectedIndexChanged(null, null);
                    ddlVILLAGETOWNBinding();
                    txtVILLAGETOWN.Text = entity.VILLAGETOWN;
                    txtADDRESS.Text = entity.ADDRESS;
                }
                catch
                {

                }
                #endregion

                PageHelper.SetControlValue<T_CUSTOMER_Entity>(this.phCustomerInfo, entity);
                lblCustomerCode.Text = entity.CUSTOMERCODE;
                hfCustomerId.Value = entity.CUSTOMERID.Trim();

            }
            
        }

        /// <summary>
        /// Handles the Click event of the btnBlackApply control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnBlackApply_Click(object sender, EventArgs e)
        {
            

        }


        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if(btn == null)
            {
                return;
            }

            pConsultingCase.Visible = false;
            pComplaintCase.Visible = false;
            pRepairCase.Visible = false;
            pPotentialCustomerCase.Visible = false;

            switch (btn.ID)
            {
                case "btnAddConsultingCase":
                    {
                        UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + _userLoginInfo.UserEntity.USERID.ToString(), "WorkOrder_btnAddConsultingCaseBegin");
                        pConsultingCase.Visible = true;
                        Label lbl = consultingCase.FindControl("lblConsultingCaseID") as Label;
                        if (lbl != null)
                        {
                            lbl.Text = "";
                        }
                        consultingCase.pageInit();
                        consultingCase.LoadButton();
                        UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + _userLoginInfo.UserEntity.USERID.ToString(), "WorkOrder_btnAddConsultingCaseEnd");
                    }
                    break;
                case "btnAddRepairCase":
                    {
                        UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + _userLoginInfo.UserEntity.USERID.ToString(), "WorkOrder_btnAddRepairCaseBegin");
                        pRepairCase.Visible = true;
                        Label lbl = repairCase.FindControl("lblRepairCaseID") as Label;
                        if (lbl != null)
                        {
                            lbl.Text = "";
                        }
                        repairCase.pageInit();
                        repairCase.LoadButton();
                        UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + _userLoginInfo.UserEntity.USERID.ToString(), "WorkOrder_btnAddRepairCaseEnd");
                    }
                    break;
                case "btnAddComplaintCase":
                    {
                        UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + _userLoginInfo.UserEntity.USERID.ToString(), "WorkOrder_btnAddComplaintCaseBegin");
                        pComplaintCase.Visible = true;
                        Label lbl = complaintCase.FindControl("lblComplaintCaseID") as Label;
                        if (lbl != null)
                        {
                            lbl.Text = "";
                        }
                        complaintCase.pageInit();
                        complaintCase.LoadButton();
                        UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + _userLoginInfo.UserEntity.USERID.ToString(), "WorkOrder_btnAddComplaintCaseEnd");
                    }
                    break;
                case "btnAddPotentialCustomerCase":
                    {
                        UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + _userLoginInfo.UserEntity.USERID.ToString(), "WorkOrder_btnAddPotentialCustomerCaseBegin");
                        pPotentialCustomerCase.Visible = true;
                        Label lbl = potentialCustomerCase.FindControl("lblPotentialCustomerCaseID") as Label;
                        if (lbl != null)
                        {
                            lbl.Text = "";
                        }
                        potentialCustomerCase.pageInit();
                        potentialCustomerCase.LoadButton();
                        UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + _userLoginInfo.UserEntity.USERID.ToString(), "WorkOrder_btnAddPotentialCustomerCaseEnd");
                    }
                    break;
            }




        }



        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            UserLoginInfo.WriteWorkOrderLog("WorkOrder_" + _userLoginInfo.UserEntity.USERID.ToString(), String.Format("WorkOrder_OnPreRender----Begin:{0}--End:{1}--Diff:{2}----------------------------", beginDateTime.ToString("HH:mm:ss.fff"), DateTime.Now.ToString("HH:mm:ss.fff"), DateTime.Now.Subtract(beginDateTime)));
        }
    }
}
